package com.library.web.borrow_book.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.library.annotation.Auth;
import com.library.jwt.JwtUtils;
import com.library.utils.ResultUtils;
import com.library.utils.ResultVo;
import com.library.web.borrow_book.pojo.*;
import com.library.web.borrow_book.service.BorrowBookService;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/borrow")
public class BorrowBookController {
    @Autowired
    private BorrowBookService borrowBookService;
    @Autowired
    private JwtUtils jwtUtils;

    //图书审核
    @PutMapping("/examinBook")
    @Auth
    public ResultVo examinBook(@RequestBody BorrowBook borrowBook) {
        borrowBook.setBorrowStatus("1");
        borrowBook.setApplyStatus("1");
        boolean b = borrowBookService.updateById(borrowBook);
        if (b) {
            return ResultUtils.success("审核成功!");
        } else {
            return ResultUtils.error("审核失败!");
        }
    }

    //借书列表
    @PostMapping
    @Auth
    public ResultVo borrow(@RequestBody BorrowParm parm, HttpServletRequest request) {
        //获取token
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            return ResultUtils.error("token过期", 600);
        }
        //获取当前用户类型
        Claims claims = jwtUtils.getClaimsFromToken(token);
        String userType = (String) claims.get("userType");
        borrowBookService.borrow(parm, userType);
        return ResultUtils.success("借书成功!");
    }

    //还书列表
    @Auth
    @GetMapping("/getBorrowList")
    public ResultVo getBorrowList(ListParm parm, HttpServletRequest request) {
        //获取token
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            return ResultUtils.error("token过期", 600);
        }
        Claims claims = jwtUtils.getClaimsFromToken(token);
        String userType = (String) claims.get("userType");
        IPage<ReturnBook> borrowList = null;
        if (userType.equals("0")) {//0是读者
            borrowList = borrowBookService.getReaderBorrowList(parm);
            return ResultUtils.success("查询成功", borrowList);
        } else if (userType.equals("1")) {//1是管理员
            borrowList = borrowBookService.getBorrowList(parm);
            return ResultUtils.success("查询成功", borrowList);
        } else {
            return ResultUtils.success("查询成功", borrowList);
        }
    }

    //还书
    @Auth
    @PostMapping("/returnBook")
    public ResultVo returnBook(@RequestBody List<ReturnParm> list) {
        borrowBookService.returnBook(list);
        return ResultUtils.success("还书成功!");
    }

    //异常还书
    @Auth
    @PostMapping("/exceptionBooks")
    public ResultVo exceptionBooks(@RequestBody ExceptionParm parm) {
        borrowBookService.exceptionBook(parm);
        return ResultUtils.success("还书成功!");
    }

    //审核拒绝
    @Auth
    @PostMapping("/refuseExamineBooks")
    public ResultVo refuseExamineBooks(@RequestBody RefuseExamineParm parm) {
        borrowBookService.refuseExamineBook(parm);
        return ResultUtils.success("拒绝审核成功!");
    }

    //借阅查看
    @Auth
    @GetMapping("/getLookBorrowList")
    public ResultVo getLookBorrowList(LookParm parm, HttpServletRequest request) {
        //获取token
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            return ResultUtils.error("token过期", 600);
        }
        Claims claims = jwtUtils.getClaimsFromToken(token);
        String userType = (String) claims.get("userType");
        IPage<LookBorrow> lookBorrowList = null;
        if (userType.equals("0")) {//0是读者
            lookBorrowList = borrowBookService.getReaderLookBorrowList(parm);
            return ResultUtils.success("查询成功", lookBorrowList);
        } else if (userType.equals("1")) {//1是管理员
            lookBorrowList = borrowBookService.getLookBorrowList(parm);
            return ResultUtils.success("查询成功", lookBorrowList);
        } else {
            return ResultUtils.success("查询成功", lookBorrowList);
        }
    }

    //借书续期
    @Auth
    @PutMapping("/addBookTime")
    public ResultVo addBookTime(@RequestBody BorrowParm parm) {
        BorrowBook borrowBook = new BorrowBook();
        borrowBook.setBorrowId(parm.getBorrowId());
        borrowBook.setReturnTime(parm.getReturnTime());
        boolean b = borrowBookService.updateById(borrowBook);
        if (b) {
            return ResultUtils.success("续期成功");
        } else {
            return ResultUtils.error("续期失败");
        }
    }
}
